import { request } from "@umijs/max";


export class ArticleApi {
    static async list(params?: {
        keyword?: string;
        current?: number;
        pageSize?: number;
    } & Record<string, String | number | null>,
        options?: { [key: string]: any },) {
        let data = await request<Cms.PageInfo<Cms.ArtileList>>("/api/cms/manager/article/", {
            method: 'GET',
            params: {
                page: params?.current,
                page_size: params?.pageSize,
                ...params,
            },
            ...(options || {}),
        });
        return {
            data: data.datas || [],
            success: true,
            total: data.total_items,
        }
    }

    static async create(body?: any,
        options?: { [key: string]: any },) {
        return request<{ id: number | String }>("/api/cms/manager/article/", {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            data: body,
            ...(options || {}),
        });
    }

    static async update(id: number | String, body?: any, options?: { [key: string]: any },) {
        return request<{ id: number | String }>(`/api/cms/manager/article/${id}`, {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json',
            },
            data: body,
            ...(options || {}),
        });
    }

    static async recycle(id: number | String, options?: { [key: string]: any },) {
        return request<{ id: number | String }>(`/api/cms/manager/article/${id}/recycle`, { method: 'PUT', ...(options || {}), });
    }

    static async unrecycle(id: number | String, options?: { [key: string]: any },) {
        return request<{ id: number | String }>(`/api/cms/manager/article/${id}/restore`, { method: 'PUT', ...(options || {}), });
    }

    static async delete(id: number | String, options?: { [key: string]: any },) {
        return request<{ id: number | String }>(`/api/cms/manager/article/${id}`, { method: 'DELETE', ...(options || {}), });
    }

    static async fetch(id: number | String, options?: { [key: string]: any },) {
        return request<any>(`/api/cms/manager/article/${id}`, { method: 'GET', ...(options || {}), });
    }

    static async publish(id: number | String, options?: { [key: string]: any }) {
        return request<any>(`/api/cms/manager/article/publish/${id}`, { method: 'PUT', ...(options || {}), });
    }

    static async unpublish(id: number | String, options?: { [key: string]: any }) {
        return request<any>(`/api/cms/manager/article/unpublish/${id}`, { method: 'PUT', ...(options || {}), });
    }
}


export class CategoryApi {
    static async list(options?: { [key: string]: any },) {
        return await request<Cms.Category[]>("/api/cms/manager/category/", {
            method: 'GET',
            ...(options || {}),
        })
    }
    static async fetch(id: number, options?: { [key: string]: any },) {
        return request<Cms.Category>(`/api/cms/manager/category/${id}`, {
            ...(options || {}),
        });
    }
    static async create(body?: Cms.Category,
        options?: { [key: string]: any },) {
        return request<any>("/api/cms/manager/category/", {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            data: body,
            ...(options || {}),
        });
    }

    static async update(id: number, body?: Cms.Category, options?: { [key: string]: any },) {
        return request(`/api/cms/manager/category/${id}`, {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json',
            },
            data: body,
            ...(options || {}),
        });
    }
    static async delete(id: number, options?: { [key: string]: any },) {
        return request(`/api/cms/manager/category/${id}`, { method: 'DELETE', ...(options || {}), });
    }
}